'use strict';

const Controller = require('egg').Controller;

class UserService extends Controller {
  // 查询账户名
  async getUserByName(username) {
    const { app } = this;

    try {
      return await app.mysql.get('user', { username });
    } catch (e) {
      console.log(e);
      return null;
    }
  }

  async editUserInfo(params) {
    const { ctx, app } = this;

    try {
      // 通过 app.mysql.update 方法，指定 user 表，
      return await app.mysql.update('user', params, { id: params.id });
    } catch (e) {
      console.log(e);
      return null;
    }
  }

  // 注册
  async register(params) {
    const { app } = this;

    try {
      return await app.mysql.insert('user', params);
    } catch (e) {
      console.log(e);
      return null;
    }
  }
}

module.exports = UserService;
